<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;

/**
 * area action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class AreaAction extends CommonAction {

    protected $serviceBean = 'system.area.service';
    protected $actionTitle = '地区';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $name = $request->getParameter('name', 'trim');

        $conditions = array();
        if ( $name ) $conditions['name'] = "%{$name}%";

        $this->setConditions($conditions);
        $this->setOrder(array('id' => -1));
        parent::index($request);
        $this->setView('system/area_index');
        $this->assign('actionTitle', $this->actionTitle);
        $this->assign("title", $this->actionTitle."列表");
        $this->assign('breadcrumb', array('系统管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
    }

    //数据添加页面
    public function add(HttpRequest $request) {
        throw new UnSupportedOperationException('暂时不支持该操作.');
    }

    //数据编辑页面
    public function edit(HttpRequest $request) {
        $id = $request->getParameter('id', 'intval');
        if ( $id <= 0 ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        $service = Beans::get($this->getServiceBean());
        $item = $service->getItem($id);
        if ( empty($item) ) {
            AjaxResult::ajaxResult(AjaxResult::OP_FAILURE, self::NO_RECORDS);
        }
        AjaxResult::ajaxResult(AjaxResult::OP_SUCCESS, null, $item);
    }

    //插入数据操作
    public function insert(HttpRequest $request) {

        $data = $request->getParameter('data');
        $data['create_date'] = date('Y-m-d H:i:s');
        $data['modify_date'] = date('Y-m-d H:i:s');
        parent::__insert($data);
    }

    //更新数据操作
    public function update(HttpRequest $request) {

        $data = $request->getParameter('data');
        $id = $request->getParameter('id', 'intval');
        $data['modify_date'] = date('Y-m-d H:i:s');
        parent::__update($data, $id);

    }

    /**
     * 创建JS文件
     * @param HttpRequest $request
     */
    public function createJSFile(HttpRequest $request) {

        $service = Beans::get($this->getServiceBean());
        if ( $service->createJsFile() != false ) {
            //记录日志
            $request->addParameter('logmessage', '更新了地区js缓存文件');
            AjaxResult::ajaxSuccessResult();
        } else {
            AjaxResult::ajaxFailtureResult();
        }

    }
}
